#include<iostream>
#include<thread>
using namespace std;

class A {
public:
    mutable int num;//标注为const也能修改
    //类型转换构造函数，可以把一个int转换成一个类A对象
    A(int num) :num(num) {
        cout << "构造函数执行" << this << "threadid=" << std::this_thread::get_id() << endl;
    }
    A(const A& a) :num(a.num) {
        cout << "拷贝构造函数执行" << this << "threadid=" << std::this_thread::get_id() << endl;
    }
    ~A() {
        cout << "析构函数执行" << this << "threadid=" << std::this_thread::get_id() << endl;
    }
};

void test(const A& a) {
    a.num = 199;
    cout << "线程test的a.num=" << a.num << endl;
    cout << "线程test的参数地址是" << &a << "threadid=" << std::this_thread::get_id() << endl;
}

int main() {
    cout << "主线程id是" << std::this_thread::get_id() << endl;
    A a(10);
    //thread th(test, a);//将类对象作为线程参数  //修改不会影响main的值
    //将线程传入参数加上std::ref
    thread th(test, std::ref(a));
    //主线程的a.num = 199,被成功修改
    th.join();
    cout << "主线程的a.num=" << a.num << endl;
    cout << "主线程执行完毕" << endl;
    return 0;
}
/* ————————————————
版权声明：本文为CSDN博主「LeetCode刷题记录」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/qq_42147969/article/details/118325905 */